#include "Turtle.h"
#include "ResourceManager.h"
#include <d3dx9.h>
#include "Texture.h"

Turtle::Turtle(int id, float x, float y, int type, int state,int direction)
{
	
	check_turn = false;

	_sprite = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Turtle_Green), 150, 0);
	_id =id;
	_x = x;
	_y = y;
	_type = type;
	_vx = 0;
	_vy = 0;
	_width = _sprite->getTexture()->getWidth();
	_height = _sprite->getTexture()->getHeight();
	_accelY = 0;
	_xPre = x;
	_yPre = y;
	_state = state;
	_timeStop = 0;
	isGravity = true;
	_direction = direction;
	if (_state == ES_ACTIVING)
	{
		if (direction == 1)			//sang trai
		{
			_vx = -(float)TURTLE_VX;
		} 
		else if (direction == 2)	//sang phai
		{
			_vx = (float)TURTLE_VX;
		}
	}
	else if (_state == ES_STOP)
	{
		_timeStop = 0;
		_vx = 0;
		_sprite->SetFrame(6,7);
		
	}
	else if (_state == ES_MOVE_SHELL_LEFT)
	{
		_sprite->SetFrame(6,8);

		_vx = -TURTLE_VX * 5;
		_accelY = TURTLE_GRAVITY;

		 
	}
	else if (_state == ES_MOVE_SHELL_RIGHT)
	{
		_sprite->SetFrame(6,8);

		_vx = TURTLE_VX * 5;
		_accelY = TURTLE_GRAVITY;


	}
}

Turtle::~Turtle(void)
{

}

void Turtle::Update(list<Object *> *_list, float time)
{
	list<Object*>::iterator i;
	Object* obj;

	switch (_state)
	{
	case ES_ACTIVING:
		Move(_list, time);

		if (_y <= 0)	//roi xuong ho, height = 0
		{
			changeState(ES_REMOVE);
		}
		_sprite->Update(time);
		break;

	case ES_MOVE_SHELL_LEFT:
	case ES_MOVE_SHELL_RIGHT:
		Move(_list, time);
		if (_y <= 0)	//roi xuong ho, height = 0
		{
			changeState(ES_REMOVE);
		}
		_sprite->Update(time);
		break;
	case ES_STOP:
		_timeStop += time;
		if (_timeStop > 2900)
		{
			_timeStop -= 2900;
			changeState(ES_ACTIVING);
		}
		_sprite->Update(time);
		
		break;
	case ES_FALL:
		_x += _vx * time;
		_vy -= _accelY * time;
		_y += _vy * time + 1/2 * _accelY * time *time;
		
		if (_y <= 0)	//roi xuong ho, height = 0
		{
			changeState(ES_REMOVE);
		}
		_sprite->Update(time);
		break;
	case ES_FALLBULLET:
		_x += _vx * time;
		_vy -= _accelY * time;
		_y += _vy * time + 1/2 * _accelY * time *time;

		if (_y <= 0)	//roi xuong ho, height = 0
		{
			changeState(ES_REMOVE);
		}
		_sprite->Update(time);

		break;
	}

}

void Turtle:: Render(int vpX, int vpY)
{
	if (_state != ES_REMOVE)
	{
	_sprite->Render(_x, _y, vpX, vpY);
	}
}

void Turtle ::Move(list<Object*>* _list, float time)
{
	list<Object*>::iterator i;
	Object* obj;

	//Xet va cham theo truc X
	_x += _vx * time;
	
	//Xet va cham theo truc Y
	if (isGravity == false)
	{
		_accelY = 0;
	}
	else
	{
		_accelY = TURTLE_GRAVITY;
	}	
	_vy -= _accelY * time;
	_y += _vy * time + 1/2.0 * _accelY * time * time;


	for (i = _list->begin(); i != _list->end(); i++)
	{
		obj = *i;
		if (obj->getObjectType() == OBJECT_LAND)
		{
			Box box(this->_xPre,
				this->_yPre - this->_height, 
				_width, 
				_height, 
				_vx * time, 
				_vy * time + 1.0f/2.0f * _accelY * time * time);

			Box block(obj->getPosX(),
				obj->getPosY() - obj->getHeight(),
				obj->getWidth(),
				obj->getHeight()
				);

			Box boxphase = Collision::_GetSweptBroadphaseBox(box);
			if (Collision::_AABBCheck(boxphase, block))
			{
				float normalx = 0;
				float normaly = 0;
				float collision_time = Collision::_SweptAABB(box, block, normalx, normaly);
				if (0 < collision_time && collision_time < 1)
				{
					
					if (normalx == 1 && normaly == 0)		//va cham tu phai -> trai
					{
						_x = _xPre + _vx * collision_time;
						_accelX = 0;
						_vx = -_vx;
					} 
					else if (normalx == -1 && normaly == 0)		//va cham tu trai -> phai
					{
						_x = _xPre + _vx * collision_time;
						_accelX = 0;
						_vx = -_vx;

					}
					else if (normalx == 0 && normaly == -1)		//va cham tu duoi -> tren
					{
					}
					else if (normalx == 0 && normaly == 1)		//va cham tu tren -> duoi
					{
						_y = _yPre + _vy * collision_time + 1.0f/2.0f * _accelY * collision_time *collision_time + 0.5;
						_accelY = 0;
						_vy = 0;

					}

					if (this->_yPre - this->_height <= obj->getPosY() + 1.5)
					{
						isGravity = false;
					}
					else
					{
						isGravity = true;
					}
					
				}
				else
				{
				}
			}
			else
			{
				if (this->_yPre - this->_height > obj->getPosY() + 1.5)
				{
					isGravity = true;
				}
				
			}
		}
	}

	if(_vx > 0)
	{
		TurnRight();
	}
	else
	{
		TurnLeft();
	}
	_xPre = _x;
	_yPre = _y;

	if (_vx >= 0)	//sang phai
	{
		_direction = 2;
	} 
	else
	{
		_direction = 1;
	}
}

void Turtle :: TurnLeft()
{
	check_turn = true;

	if (_state == ES_MOVE_SHELL_RIGHT || _state == ES_MOVE_SHELL_LEFT)
	{
		 
		_sprite->SetFrame(6, 9);
	}
	else	//state active
	{
		_sprite->SetFrame(0, 2);
		 
	}
}


void Turtle :: TurnRight()
{
	check_turn = false;
	 

	if (_state == ES_MOVE_SHELL_RIGHT || _state == ES_MOVE_SHELL_LEFT)
	{
		 
		_sprite->SetFrame(6, 9);
	}
	else	//state active
	{
		_sprite->SetFrame(2, 4);
		 
	}
}

void Turtle::changeState(int state)
{
	_state = state;
	switch (_state)
	{
	case ES_ACTIVING:
		if (check_turn == false)	 //turn right
		{
			_vx = TURTLE_VX;
		} 
		else						//turn left
		{
			_vx = -TURTLE_VX;
		}
		 
		_accelY = TURTLE_GRAVITY;
		break;

	case ES_FALL:
		if (check_turn == false)	 //turn right
		{
			_sprite->SetFrame(5,6);
		} 
		else						//turn left
		{
			_sprite->SetFrame(4,5);
		}
		_vy = 1.5;
		_accelY = TURTLE_GRAVITY;
		 
		break;

	case ES_FALLBULLET:
		if (check_turn == false)	 //turn right
		{
			_sprite->SetFrame(10,10);
		} 
		else						//turn left
		{
			_sprite->SetFrame(9,9);
		} 
		_vy = 1.5;
		_accelY = TURTLE_GRAVITY;

		break;

	case ES_MOVE_SHELL_LEFT:
		_sprite->SetFrame(6,8);
		_x -= 1;
		_vx = -TURTLE_VX * 3;
		_accelY = TURTLE_GRAVITY;
		break;
	case ES_MOVE_SHELL_RIGHT:
		_sprite->SetFrame(6,8);
		_x += 1;
		_vx = TURTLE_VX * 3;
		_accelY = TURTLE_GRAVITY;
		break;

	case ES_STOP:
		_timeStop = 0;
		_vx = 0;
		 
		_sprite->SetFrame(6,7);
		break;
	}
}
OBJECT_TYPE Turtle::getObjectType()
{
	return OBJECT_ENEMY;
}